package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.dto.BulletinDto;
import com.orientation.entity.Bulletin;
import com.orientation.service.BulletinService;
import com.orientation.vo.BulletinStudentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-02-12
 */
@Api(tags = "消息通告")
@Validated
@RestController
@RequestMapping("/bulletin")
public class BulletinController {
    @Resource
    private BulletinService bulletinService;

    @ApiOperation("添加")
    @PostMapping
    public boolean save(@RequestBody Bulletin bulletin) {
        return bulletinService.save(bulletin);
    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Bulletin bulletin) {
        return bulletinService.updateById(bulletin);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return bulletinService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Bulletin findById(@PathVariable String id) {
        return bulletinService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Bulletin> findAll() {
        return bulletinService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<Bulletin> findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return bulletinService.page(new Page<>(pageNum, pageSize));
    }

    @ApiOperation("批量通告")
    @PostMapping("/saveBatchBulletin")
    public ResponseEntity<Result> saveBatchBulletin(@RequestBody BulletinDto bulletinDto) {
        bulletinService.saveBatchBulletin(bulletinDto);
        return ResponseEntity.ok(new Result());
    }


    @ApiOperation("通告列表")
    @GetMapping("/queryBulletinStudent")
    public ResponseEntity<Result> queryBulletinStudent(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                       @RequestParam(value = "majorId", required = false) String majorId,
                                                       @RequestParam(value = "className", required = false) String className,
                                                       @RequestParam(value = "studentId", required = false) String studentId,
                                                       @RequestParam(value = "createTime", required = false) String createTime,
                                                       @RequestParam(defaultValue = "") String adviserId) {
        IPage<BulletinStudentVo> bulletinStudentVoIPage = bulletinService.queryBulletinStudent(pageNum, pageSize, majorId, className, studentId, adviserId, createTime);
        return ResponseEntity.ok(Result.success(bulletinStudentVoIPage));
    }
}

